#include "stdafx.h"
#include "service\storage\file\BaseFileService.h"
#include <fstream>


BaseFileService::BaseFileService() {
  file_ = new std::fstream();
}


BaseFileService::BaseFileService(const std::string fname,
                                 const std::ios::openmode mode) {
  file_ = new std::fstream();
  Open(fname.c_str(), mode);

}


BaseFileService::BaseFileService(std::fstream& file) {
  file_ = &file;
  ++refcount_;

}


BaseFileService::~BaseFileService() {
  --refcount_;
  if (!refcount_)
    delete file_;
}


void BaseFileService::set_file(std::fstream& file) {
}


std::fstream& BaseFileService::file() {
  return *file_;
}


BaseFileService& BaseFileService::Open(const std::string fname,
                                       const std::ios::openmode mode) {
  return Open(fname.c_str(), mode);
}


BaseFileService& BaseFileService::Open(const char *fname,
                                       const std::ios::openmode mode) {
  this->Close();
  file_->open(fname, mode);
  return *this;
}


BaseFileService& BaseFileService::OpenOut(const std::string fname,
                                          const std::ios::openmode mode) {
  return OpenOut(fname.c_str(), mode);
}


BaseFileService& BaseFileService::OpenOut(const char* fname,
                                          const std::ios::openmode mode) {
  this->Close();
  file_->open(fname, mode);
  return *this;
}


BaseFileService& BaseFileService::OpenIn(const std::string fname,
                                         const std::ios::openmode mode) {
  return OpenIn(fname.c_str(), mode);
}


BaseFileService& BaseFileService::OpenIn(const char* fname,
                                         const std::ios::openmode mode) {
  this->Close();
  file_->open(fname, mode);
  return *this;
}


BaseFileService& BaseFileService::Close() {
  if (file_->is_open())
    file_->close();
  return *this;
}